<?php

class UserPower
{

  var  $filename = "";          //
  
  var   $usermsg= "";//用户信息
  
  var   $powerdata= array();//
  
  var   $is_oneuserpower= 0;// 
  
  var   $catdata= array('search'=>'group','editpost'=>'alonepage','addpost'=>'alonepage','editpost'=>'subject','addpost'=>'subject');//  

 function __construct($filename,&$usermsg = null)
    {	
	
        $this->UserPower($filename,$usermsg);
				
    }
	   
 function UserPower($filename,&$usermsg)
    {	
	
        $this->filename = $filename;	
        $this->usermsg = &$usermsg;	
			
	}
	
 //用户组权限判断
 function Power($app,$act)
    {
        $this->OneUserPower($app,$act);	
			
        $this->GetPowerData();

        $this->_Power($app,$act);
		
        $this->PersonPower();	
	}
	

 //用户组权限判断
 function _Power($app,$act,$powername = '')
    {
	    if(!isset($this->usermsg['user_id']))
		{
		 $this->usermsg['user_id'] = 0;
		}
	    if(!isset($this->usermsg['data']))
		{
		 $this->usermsg['data'] = array();
		}		
	    if(!isset($this->usermsg['data']['role_id']))
		{
		 $this->usermsg['data']['role_id'] = ROLE_Y_ID;//默认为游客组
		}	  	    
	    if($this->usermsg['user_id']==0)
		{
		$this->usermsg['data']['role_id'] = ROLE_Y_ID;//默认为游客组
		}
        if(empty($powername))
		{
		  $powername = $GLOBALS['SCup']->SCup_name;
		}
		$role_array = role_deal($this->usermsg['data']['role_id']);
		
		$role_id = $role_array[$GLOBALS['SCup']->SCup_name];


	    if(!isset($this->powerdata[$powername][$role_id]))
		{
		 $notice = &get_message(array('msg'=>'no exist powerdata','url'=>'','title'=>'Notice'));
		 $notice ->display();		
		}
		if(empty($this->is_oneuserpower))
		{
		     $icon = 0;	
		     if(!isset($_GET['cat']))
		     {
                 $_GET['cat'] ='0';
		     }
		     if(!isset($_POST['data']['cat_id']))
		     {
		         $icon =1;
                 $_POST['data']['cat_id'] ='0';
		     }
		     if(empty($_GET['cat']))
		     {
		         $cat = '0';			
		     }
		     else
		     {
		         $cat = intval($_GET['cat']);		
		     }
								
		     if(empty($cat))
		     {
		         $cat = intval($_POST['data']['cat_id']);		
		     }
				
		     if(isset($this->catdata[$act])&&$this->catdata[$act]==$app)
		     {
		         $cat = '0';		
		     } 	
        
			
		     if((!(isset($this->powerdata[$powername][$role_id]['powerdb'][$app.':'.$cat]) && isset($this->powerdata[$powername][$role_id]['powerdb'][$app.':'.$cat][$act]) && isset($this->powerdata[$powername][$role_id]['powerdb'][$app.':'.$cat][$act]['set']) && $this->powerdata[$powername][$role_id]['powerdb'][$app.':'.$cat][$act]['set']==1)))
		     {
				 if(!isset($this->powerdata[$powername][$role_id]['powerdb'][$app.':'.$cat][$act]['url']))
				 {
				 $this->powerdata[$powername][$role_id]['powerdb'][$app.':'.$cat][$act]['url'] = '';
				 }
		         $notice = &get_message(array('msg'=>'no this power','url'=>$this->powerdata[$powername][$role_id]['powerdb'][$app.':'.$cat][$act]['url'],'title'=>'Notice'));
		         $notice ->display();				 
		     }
		//主要用来判断用户组是否可以开启div视图编辑功能,然后再页面中开启视图编辑的开关功能
		     if(isset($this->powerdata[$powername][$role_id]['powerdb']['do:0']['divstart']['set'])&&$this->powerdata[$powername][$role_id]['powerdb']['do:0']['divstart']['set']==1)
		    {
		         define('DIV_START_VIEW',1);
		    }
		    
		    if($icon)
		    {
		         unset($_POST['data']['cat_id']);
		    }	
	
		}

        
		$GLOBALS['SCup']->SCup_user_msg['role_name'] =  $this->powerdata[$powername][$role_id]['role_name'];			
           
	}


	
  //单个用户权限判断
 function OneUserPower($app,$act,$powername = '')
    {
		if(!$powername)
	    {
		   $powername = $GLOBALS['SCup']->SCup_name;					   
		}	
 	    $usermsg = unserialize($this->usermsg['data']['config']);

		if(isset($usermsg['powerdb'])&&isset($usermsg['powerdb'][$powername])&&!empty($usermsg['powerdb'][$powername]))
		{
		     $icon = 0;	
		     if(!isset($_GET['cat']))
		     {
                $_GET['cat'] ='0';
		     }
		     if(!isset($_POST['data']['cat_id']))
		     {
		        $icon =1;
                $_POST['data']['cat_id'] ='0';
		     }
		     if(empty($_GET['cat']))
		     {
		        $cat = '0';			
		     }
		     else
		     {
		        $cat = intval($_GET['cat']);		
		     }
								
		     if(empty($cat))
		     {
		        $cat = intval($_POST['data']['cat_id']);		
		     }
				
		     if(isset($this->catdata[$act])&&$this->catdata[$act]==$app)
		     {
		        $cat = '0';		
		     } 	
        
			
		     if((!(isset($usermsg['powerdb'][$powername][$app.':'.$cat]) && isset($usermsg['powerdb'][$powername][$app.':'.$cat][$act]) && isset($usermsg['powerdb'][$powername][$app.':'.$cat][$act]['set']) && $usermsg['powerdb'][$powername][$app.':'.$cat][$act]['set']==1)))
		     {
				 if(!isset($usermsg['powerdb'][$powername][$app.':'.$cat][$act]['url']))
				 {
				 $usermsg['powerdb'][$powername][$app.':'.$cat][$act]['url'] = '';
				 }
		         $notice = &get_message(array('msg'=>'no this power','url'=>$usermsg['powerdb'][$powername][$app.':'.$cat][$act]['url'],'title'=>'Notice'));
		         $notice ->display();				 
		     }
		     //主要用来判断用户组是否可以开启div视图编辑功能,然后再页面中开启视图编辑的开关功能
		     if(isset($usermsg['powerdb'][$powername]['do:0']['divstart']['set'])&&$usermsg['powerdb'][$powername]['do:0']['divstart']['set']==1)
		     {
		         define('DIV_START_VIEW',1);
		     }
		
		     if($icon)
		     {
		         unset($_POST['data']['cat_id']);
		     }
			 $this->is_oneuserpower= 1;	

		}else
		{
			 $this->is_oneuserpower= 0;			
		}
		 
	
	
	
	}	

  //单个用户权限判断
 function PersonPower($powername = '')
    {
	
	
	}	
	
 function IconPowerLimit($limit=array())
    {
		$powername = $limit[0];
		/*
		个人权限判断
		*/
        if(isset($this->usermsg['data']['config']))
		{
 	        $usermsg = unserialize($this->usermsg['data']['config']);
			
		    if(isset($usermsg['powerdb'])&&isset($usermsg['powerdb'][$powername])&&!empty($usermsg['powerdb'][$powername]))
		    {			
		        if((!(isset($usermsg['powerdb'][$powername][$limit[1]]) && isset($usermsg['powerdb'][$powername][$limit[1]][$limit[2]]) && isset($usermsg['powerdb'][$powername][$limit[1]][$limit[2]]['set']) && $usermsg['powerdb'][$powername][$limit[1]][$limit[2]]['set']==1)))
		        {
 		          return 0;   				 
		        }else
				{
				  return 1; 
				}
		    }
		}		
		/*
		用户组权限判断
		*/			
        $this->GetPowerData($powername);
		
	    if(!isset($this->usermsg['user_id']))
		{
		 $this->usermsg['user_id'] = 0;
		}
	    if(!isset($this->usermsg['data']))
		{
		 $this->usermsg['data'] = array();
		}		
	    if(!isset($this->usermsg['data']['role_id']))
		{
		 $this->usermsg['data']['role_id'] = ROLE_Y_ID;//默认为游客组
		}	  	    
	    if($this->usermsg['user_id']==0)
		{
		$this->usermsg['data']['role_id'] = ROLE_Y_ID;//默认为游客组
		}

		$role_array = role_deal($this->usermsg['data']['role_id']);		
		$role_id = $role_array[$powername];
			
		if((!(isset($this->powerdata[$powername][$role_id]['powerdb'][$limit[1]]) && isset($this->powerdata[$powername][$role_id]['powerdb'][$limit[1]][$limit[2]]) && isset($this->powerdata[$powername][$role_id]['powerdb'][$limit[1]][$limit[2]]['set']) && $this->powerdata[$powername][$role_id]['powerdb'][$limit[1]][$limit[2]]['set']==1)))
		{
 		return 0;         				 
		}		    
		return 1;
	}	
	
  //
 function GetPowerData($powername = '')
    {	
	    if(!isset($this->usermsg['data']['role_id']))
		{
		  $this->usermsg['data']['role_id'] = ROLE_Y_ID;
		}

		$role_array = role_deal($this->usermsg['data']['role_id']);		

		if(empty($this->filename))
	    {
		   $powername = $GLOBALS['SCup']->SCup_name;
		   $role_id = $role_array[$powername];		   	   			
		   $this->filename = ROOT_PATH . '/data/power/'.$powername.'_'.$role_id.'.power.php';					 
		}else
		{
		   if($powername)
	       {
		    $role_id = $role_array[$powername];		   
		    $this->filename = ROOT_PATH . '/data/power/'.$powername.'_'.$role_id.'.power.php';					 
		   }else
		   {
		    $powername = $GLOBALS['SCup']->SCup_name;
		    $role_id = $role_array[$powername];								   
		   }		
		}	
	     	  
		if(is_file($this->filename))
	    {
		   if(!isset($this->powerdata[$powername]))
		   {
	         $this->powerdata[$powername] = include($this->filename); 
	   
		   } 
		}
		else
	    {
		   $filename = $GLOBALS['SCup']->user_power_file;
		   if(is_file($filename))
	       {
		      if(!isset($this->powerdata[$powername]))
		      {
	             $this->powerdata[$powername] = include($filename); 	   
		      } 	   
		   }
		   else
	       {
            exit('Missing PowerFile');
		   } 
		}  
	
	}

}


?>